package com.jk.web.log;

import com.alibaba.fastjson.JSON;
import com.jk.common.model.LogBean;
import com.jk.common.model.MessageBean;
import com.jk.common.model.OrderBean;
import com.jk.web.messager.LogSender;
import com.jk.web.service.OrderService;
import com.jk.web.webclient.WebSocketServer;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

@Aspect
@Component
public class LogAop {

    @Autowired
    private LogSender logSender;

    @Autowired
    private OrderService orderService;

    @Autowired
    private WebSocketServer webSocketServer;

    //扫描这个类中的方法
    @Pointcut(value = "execution(* com.jk.web.controller.MssagerControllrt.*(..))")
    public void logCut(){
    }
    @AfterReturning(pointcut = "logCut()" ,returning = "obj")
    public void saveLog(JoinPoint joinPoint,Object obj) throws IOException {
        System.out.println("生成日志");
        MessageBean messageBean = new MessageBean();

        StringBuffer str = new StringBuffer();
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            str.append(args[0]);
        }
        System.out.println(str.toString());
        //根据流水号查询mongdb数据库
        OrderBean orderBean = orderService.orderNumber(str.toString());
        //消息发送时间
        Date date = new Date();
        String s = String.valueOf(date);

        messageBean.setOrderNumber("订单号"+str.toString());//订单号
       /* messageBean.setDeliveryDate("送达日期"+orderBean.getDeliveryDate()); //送达日期
        messageBean.setDeliveryTime("送达时段"+orderBean.getDeliveryTime()); //送达时段
        messageBean.setCustomerName("收货人名称"+orderBean.getCustomerName());//收货人名称
        messageBean.setCustomerPhone("收货人手机号"+orderBean.getCustomerPhone());//（收货人手机号）
        messageBean.setCustomerAddress("收货地址"+orderBean.getCustomerAddress());//(收货地址 精确到门牌号)
        messageBean.setShengText("所在省名称"+orderBean.getShengText());//（所在省名称）
        messageBean.setShiText("所在市名称"+orderBean.getShiText());//（所在市名称）
        messageBean.setQuText("所在区名称"+orderBean.getQuText());//（所在区名称）
        messageBean.setFlowerName("鲜花名称"+orderBean.getFlowerName());//(鲜花名称)
        messageBean.setFlowerLanguage("备注花语"+orderBean.getFlowerLanguage());//(备注花语)
        messageBean.setFlowerPrice("鲜花价格"+orderBean.getFlowerPrice());//(鲜花价格)*/


        String s1 = JSON.toJSONString(messageBean);

        logSender.send(s1);

    }
}
